package com.ccc.picture.controller;

import com.ccc.picture.util.ImageCutUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author superC
 * @version 1.0.0
 * @date 2024/7/18
 */

@RestController
@RequestMapping("/img/cut")
public class ImgCutController {
    protected static final Logger logger = LoggerFactory.getLogger(ImgCutController.class);

    /**
     * 图片裁剪，返回裁剪后的图片
     *
     * @param height 需要裁剪的高度
     */
    @PostMapping(value = "/handle", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void handle(@RequestParam Integer height, @RequestParam MultipartFile file, HttpServletResponse response) {
        try {
            ImageCutUtil.handle(file, height, response);
        } catch (Exception e) {
            logger.error("【捕获异常】\r\n异常记录：", e);
        }
    }

    /**
     * 图片批量裁剪，返回ZIP压缩包
     *
     * @param height 需要裁剪的高度
     */
    @PostMapping(value = "/handleBatch", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void handleBatch(@RequestParam Integer height, @RequestParam MultipartFile[] file, HttpServletResponse response) {
        try {
            ImageCutUtil.handleBatch(file, height, response);
        } catch (Exception e) {
            logger.error("【捕获异常】\r\n异常记录：", e);
        }
    }

}
